Prerequisites
- The Apache Maven tool must already be installed on your computer
- Knowing the meaning of “build” for an application
- Knowing what a Maven phase is in a build lifecycle
Short comparison with Ant
What is the difference between Maven dependency management and Ant dependency management? First of all, Ant only manages project’s build phases, delegating dependency management to Apache Ivy, with which works toghether.
Therefore, the two tools handle a different task; in detail:
- Ant runs build from build.xml file (if you want to know more, please see Apache Ant Beginners Tutorial)
- Ivy downloads dependencies (written in a list inside ivy.xml file) into local repository.
On the other hand, Maven manages both aspects (build phases and dependencies) starting from a single file called pom.xml
N.B.
Actually, Maven has built-in phases that can be managed by the developer from pom.xml file. On the other hand, woking with Ant, phases must be created from the build.xml file.
How Maven dependencies work
The image below, show Maven dependencies workflow during the entire build process.
Each step (represented by an arrow) move the dependecy from one point to the next. A step is run by a phase execution.
Maven dependency management come from the pom.xml file, inside project’s root folder. The dependencies list is written in the pom.xml.
This is a simple example:
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
Dependencies list start with <dependencies> tag and end with </dependencies> (row 10 and row 22).
A single dependency start with <dependency> tag and end with </dependency> and is uniquely identified by the three element <groupId>, <artifactId> e <version> both in Maven Central Repository (or others external) and in the local repository (after the download).
Compile phase
During compile phase, Maven downloads dependencies from Maven Central Repository ( or from other repositories written in the settings.xml files) into the local repository, the /.m2 folder. This folder is created the first time Maven downloads a dependency. From that moment all dependencies will be downloaded here. Usually /.m2 path has this structure:
>C:\Users\User Name\.m2
For example, consider a Java project containing in its root folder the pom.xml file seen in the above paragraph. From this folder, type the following maven command from cmd:
mvn compile
You should see an output like this
This means that Maven is downloading the dependencies list written in the pom.xml, from the Maven Central Repository (or from others external repositories) into the local \.m2 repository.
Package Phase
The classes and the functions inside dependencies, are used both to compile correctly the code (as shown in the previous paragraph), either to be executed during application runs. For this purpose, during this phase, project’s dependencies are copied inside the final package (that is the actual application which can be a .jar, .war, .ear), following the steps below:
- Builds and puts the final package inside the /target folder, which was created during the Compile Phase.
- Finally, dependencies are copied from the /.m2 repository, directly inside the final package, so their code can be used by the application at runtime.
From the folder containing the example pom.xml, type the following maven command from cmd:
mvn package
You should see on output similar to this one:
Dependencies are no longer downloaded during compile phase; that’s because Maven has already downloaded them in the /.m2 folder (see previous paragraph).
The jar goal from the “package phase”, creates the “projec-1-0.jar” in the /targed folder in project’s workspace. This is the final package, which contains the same dependecies used for the compile phase. Now dependencies code can be used while application runs.
Install Phase
At this point, the builded package is copied from the /target folder into the /.m2 repository. In this way the package itself can be used as a dependency by other local projects.
From the folder containing the example pom.xml, type the following maven command from cmd:
mvn install
You should see on output similar to this one, where Maven shows the install phase execution and the previous ones.